class addTe {

    constructor( changeStatusElementSelector, deleteUserElementSelector) {
        this.deleteUserEle = $(deleteUserElementSelector);
        this.changeStatusEle = $(changeStatusElementSelector);
        this.user_type = $('#user_type').attr('content');
    }

    addEventListener() {
        let that = this;
        this.changeStatusEle.on('click', function () {
            let ele = $(this);
            let id = parseInt(ele.attr('status'));
            $.ajax({
                url: '/cms/ChangeTemplateStaus/',
                type: 'post',
                data: {
                    'id': id,
                    'user_type':that.user_type,
                    'csrf_token': $('#csrf').attr('content')
                },
                success: (data) => {
                    if (data.code === 200) {
                        swal({
                            type: 'success',
                            text: '修改成功'
                        });
                        let text = ele.text().trim();
                        if (text === '启用') {
                            console.log(ele.text());
                            ele.text('禁用');
                            console.log($(id.toString()));
                            $('#' + id.toString()).replaceWith(
                                '<td id="' + id.toString() + '"><div class="tm-status-circle moving"></div>' +
                                '<b>可用</b></td>')
                        } else {
                            ele.text('启用');
                            $('#' + id.toString()).replaceWith('<td id="' + id.toString() + '"><div class="tm-status-circle cancelled"></div>\n' +
                                '                            <b>不可用</b></td>')
                        }
                    } else {
                        swal({
                            type: 'error',
                            text:data.message
                        })
                    }
                }

            })
        });
        this.deleteUserEle.on('click', function () {
            let ele = $(this);
            let id = parseInt(ele.attr('user'));
            swal({
                text: '确认删除？ 删除后不可恢复',
                showCancelButton: true,
                showConfirmButton: true,
                cancelButtonText: '取消',
                confirmButtonText: '确认，我了解'
            }).then(function (result) {
                if (result.value) {
                    $.ajax({
                        url: '/cms/ChangeTemplateStaus/',
                        type: 'delete',
                        data: {
                            id: id,
                            csrf_token: $('#csrf').attr('content'),
                            user_type:that.user_type
                        },
                        success: function (data) {
                            if (data.code === 200) {
                                swal({
                                    type: 'success',
                                    text: '删除成功'
                                });
                                $('tr[user="' + id.toString() + '"]').remove()
                            } else {
                                swal({
                                    type: 'error',
                                    text: data.message
                                });
                            }
                        },
                        error: function (error) {
                            swal({
                                type: 'error',
                                text: '网络错误'
                            });
                        }
                    })
                }
            })
        });
    }

}